package com.ask.controller.admin;

import com.ask.dto.CategoryDTO;
import com.ask.dto.CategoryQueryDTO;
import com.ask.entity.Category;
import com.ask.result.PageResult;
import com.ask.result.Result;
import com.ask.service.CategorySerivce;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategorySerivce categorySerivce;

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */
    @PostMapping
    public Result add(@RequestBody CategoryDTO categoryDTO) {
        categorySerivce.save(categoryDTO);
        return Result.success();
    }

    /**
     * 启用、禁用分类
     *
     * @param status
     * @return
     */
    @PostMapping("status/{status}")
    public Result enableOrDisable(@PathVariable("status") Integer status, Long id) {
        categorySerivce.enableOrDisable(status, id);
        return Result.success();
    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO) {
        categorySerivce.update(categoryDTO);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    public Result<List<Category>> getByType(Integer type) {
        List<Category> list = categorySerivce.getByType(type);
        return Result.success(list);
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public Result<String> delete(Long id) {
        categorySerivce.delete(id);
        return Result.success();
    }

    @GetMapping("/page")
    public Result<PageResult> pageQuery(CategoryQueryDTO categoryQueryDTO) {
        PageResult pageResult = categorySerivce.pageQuery(categoryQueryDTO);
        return Result.success(pageResult);
    }
}
